package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import sg.edu.nus.iss.ooad.jass.controller.LoginController;
import sg.edu.nus.iss.ooad.jass.entity.LoginInfo;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 * Login Screen for JASS.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 * 
 */
public class JASSLoginForm extends AbstractJASSRegularWindow {

        private static final long serialVersionUID = -5663415082006650785L;
        //Login Options
        public static final String DOMAIN_JOBSEEKER = "Jobseeker";
        public static final String DOMAIN_ADVERTISER = "Advertiser";
        public static final String DOMAIN_SYSTEM_ADMINISTRATOR = "System Administrator";
        //Form Components
        private TextField textLoginID = null;
        private TextField textLoginPassword = null;
        private Choice choiceDomain = null;
        private Label labelLoginError = null;
        private Label labelLoginID = null;
        private Label labelLoginPassword = null;
        private Label labelLoginDomain = null;
        private Button btnLogin = null;
        private Button btnReset = null;
        private Button btnCancel = null;
        private JASSRegularPanel formPanel = null;
        private Panel inputPanel = null;
        private Panel buttonPanel = null;
        private GridBagLayout inputLayout = null;
        //Form parent
        private AbstractJASSRegularWindow parent = null;
        //Singleton
        private static JASSLoginForm instance = null;

        /**
         *
         * Constructor JASSLoginForm
         * @param parent Paretn Window.
         */
        private JASSLoginForm(AbstractJASSRegularWindow parent) {
                super("JASS Login", 500, 250);
                setResizable(false);
                initComponents();
                this.parent = parent;
        }

        /**
         * Singleton Instance of the JASSLoginForm
         * @param parent
         * @return
         */

        public static JASSLoginForm getInstance(AbstractJASSRegularWindow parent) {
                if (instance == null) {
                        instance = new JASSLoginForm(parent);
                } else {
                        instance.resetForm();
                }
                return instance;
        }

        /**
         *Init all the components
         */
        private void initComponents() {
                //initiate all Components
                textLoginID = new TextField(25);
                textLoginPassword = new TextField(25);
                textLoginPassword.setEchoChar('*');
                choiceDomain = new Choice();
                choiceDomain.add(DOMAIN_JOBSEEKER);
                choiceDomain.add(DOMAIN_ADVERTISER);
                choiceDomain.add(DOMAIN_SYSTEM_ADMINISTRATOR);
                labelLoginError = new Label("", Label.LEFT);
                labelLoginError.setForeground(Color.RED);
                labelLoginID = new Label("Login ID", Label.LEFT);
                labelLoginPassword = new Label("Login Password", Label.LEFT);
                labelLoginDomain = new Label("Domain", Label.LEFT);
                btnLogin = new Button("Login");
                btnReset = new Button("Reset");
                btnCancel = new Button("Cancel");
                buttonPanel = new Panel();
                inputPanel = new Panel();
                formPanel = new JASSRegularPanel("Login Form");

                //set the action listener
                btnLogin.addActionListener(new LoginActionListener());
                btnReset.addActionListener(new ResetActionListener());
                btnCancel.addActionListener(new CancelActionListener());

                //set the layout
                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                buttonPanel.add(btnLogin);
                buttonPanel.add(btnReset);
                buttonPanel.add(btnCancel);

                inputLayout = new GridBagLayout();
                inputPanel.setLayout(inputLayout);
                attachComponent(labelLoginError, 0, 0, 2, 1);
                attachComponent(labelLoginDomain, 0, 1, 1, 1);
                attachComponent(choiceDomain, 1, 1, 1, 1);
                attachComponent(labelLoginID, 0, 2, 1, 1);
                attachComponent(textLoginID, 1, 2, 1, 1);
                attachComponent(labelLoginPassword, 0, 3, 1, 1);
                attachComponent(textLoginPassword, 1, 3, 1, 1);

                formPanel.add(inputPanel, BorderLayout.CENTER);
                formPanel.add(buttonPanel, BorderLayout.SOUTH);

                //add to the frame
                add(formPanel);
                pack();
        }


        /**
         * Attach the component to the form
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         * @param fill
         */
        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                GridBagConstraints constraint = new GridBagConstraints();
                constraint.gridx = gridx;
                constraint.gridy = gridy;
                constraint.gridwidth = gridwidth;
                constraint.gridheight = gridheight;
                constraint.fill = fill;
                constraint.insets = new Insets(4, 4, 4, 4);
                constraint.weightx = 1.0;
                constraint.weighty = 1.0;
                inputPanel.add(c);
                inputLayout.setConstraints(c, constraint);
        }

        /**
         * To reset the form
         */

        @Override
        public void resetForm() {
                textLoginID.setText("");
                textLoginPassword.setText("");
        }

        
        /**
         * Handle Login Action
         */
        class LoginActionListener implements ActionListener {

                /**
                 *
                 * @param e The Action Event
                 */
                public void actionPerformed(ActionEvent e) {

                        String loginID = textLoginID.getText();
                        String loginPassword = textLoginPassword.getText();
                        int loginDomain = choiceDomain.getSelectedIndex();

                        if (loginID == null || "".equalsIgnoreCase(loginID)) {
                                labelLoginError.setText("Please provide Login ID");
                                return;
                        }

                        if (loginPassword == null || "".equalsIgnoreCase(loginPassword)) {
                                labelLoginError.setText("Please provide Login Password");
                                return;
                        }


                        LoginController loginController = new LoginController(loginID, loginPassword, loginDomain);
                        boolean success = loginController.processLogin();
                        if (success) {
                                //forward to Jobseeker/Advertiser/System Administrator Main Screen
                                LoginInfo.getInstance(loginID, loginDomain);
                                displayMessage("Information", "Login success");
                                close();
                                parent.resetForm();
                        } else {
                                displayMessage("Error", "Login failed");
                        }
                }
        }

        /**
         * Handle Cancel Action
         */
        class CancelActionListener implements ActionListener {

                /**
                 *
                 * @param e
                 */
                public void actionPerformed(ActionEvent e) {
                        close();
                }
        }

        /**
         * Handle Reset Action.
         */
        class ResetActionListener implements ActionListener {

                /**
                 *
                 * @param e
                 */
                public void actionPerformed(ActionEvent e) {
                        choiceDomain.select(0);
                        textLoginID.setText("");
                        textLoginPassword.setText("");
                }
        } 
       
}

